
function createStage(width, height, gamerArgs) {

	let stage = document.getElementById("stage");
	stage.innerHTML = "";
	let tdArrs = [];
	let board;

	for (let i = 0; i < height; i++) {
		let tr = document.createElement("tr");
		tr.id = "tr" + i;
		for (let j = 0; j < width; j++) {
			let td = document.createElement("td");
			td.id = "td" + i + "_" + j;
			td.className = "default";
			td.onmouseup = function (e) {
				board(e, j, i);
			};
			tdArrs[i * width + j] = td;
			tr.appendChild(td);
		}
		stage.appendChild(tr);
	}

	board = mousePress(width, height, tdArrs, gamerArgs);


}

function mousePress(width, height, table, gamerArgs) {

	let press = pressEvent(width, height, table, gamerArgs);

	function getEvent(event) {
		event = event || window.event;
		event.preventDefault();
		let isIE = !event;

		let _button = null;
		if (isIE) {
			switch (event.button) {
				case 1:
					_button = "left";
					break;
				case 2:
					_button = "right";
					break;
				case 4:
					_button = "middle";
					break;
				default:
					break;
			}
		} else {
			switch (event.button) {
				case 0:
					_button = "left";
					break;
				case 1:
					_button = "middle";
					break;
				case 2:
					_button = "right";

					break;
				default:
					break;
			}
		};

		return {
			button: _button,
			returnValue: event.returnvalue
		};
	}

	document.oncontextmenu = function (e) {
		getEvent(e).returnValue = false;
		return false;
	};

	return function (e, x, y) {
		press[getEvent(e).button](x, y);
		if (press.isWin) press.isWin();
	};
}

function showResult(result, width, height, gamerArgs) {
	setTimeout(() => {
		if (confirm(`你${result}了！\n是否再来一局？`)) {
			createStage(width, height, gamerArgs);
		} else {

		}
	}, 10);
}